package com.platform.controller;

import com.platform.entity.ExchangeCodeEntity;
import com.platform.entity.GiftTypeEntity;
import com.platform.service.ExchangeCodeService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import com.platform.utils.excel.ExcelDownload;
import com.platform.utils.excel.ExcelExport;
import com.platform.utils.excel.ExcelImport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("exchangeCode")
public class ExchangeCodeController {
    @Autowired
    private ExchangeCodeService exchangeCodeService;

    /**
     * 列表
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public R list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        //查询所有
        List<ExchangeCodeEntity> exchangeCodeEntities = exchangeCodeService.queryList(query);
        //查询总条数
        int total = exchangeCodeService.queryTotal(query);
        //分页
        PageUtils pageUtil = new PageUtils(exchangeCodeEntities, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public R save(@RequestBody ExchangeCodeEntity exchangeCodeEntity){
        return exchangeCodeService.save(exchangeCodeEntity);
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public R update(@RequestBody ExchangeCodeEntity exchangeCodeEntity){
        exchangeCodeService.update(exchangeCodeEntity);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public R delete(@RequestBody Integer[] ids){
        exchangeCodeService.deleteBatch(ids);
        return R.ok();
    }

    /**
     * 查看信息
     */
    @RequestMapping(value = "/info/{id}",method = RequestMethod.GET)
    public R info(@PathVariable("id") Integer id) {
        ExchangeCodeEntity exchangeCodeEntity = exchangeCodeService.queryObject(id);
        return R.ok().put("exchangeCodeEntity", exchangeCodeEntity);
    }

    /**
     * 下载模板
     * **/
    @RequestMapping("download")
    public R download(HttpServletResponse response){
        ExcelDownload excelDownload = new ExcelDownload("兑换码导入模板");
        String[] header = new String[]{"类别名称", "卡号", "兑换码"};
        excelDownload.addSheetByMap("模板", header);
        excelDownload.export(response);
        return R.ok();
    }


    /**
     * 下载模板
     * **/
    @RequestMapping(value = "import",method = RequestMethod.POST)
    public R importExcel(@RequestParam("file") MultipartFile file){
        List<String[]> list = ExcelImport.getExcelData07(file);
        return exchangeCodeService.excelImport(list);
    }
}
